﻿using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Forms;

using SBPweb.Modules.Modularity;
using SBPweb.Modules.Communication;

namespace SBPweb.MediaCenter.Modules.Viewer.WF
{

    public class Module : Viewer.Module
	{
		private const string FILTER = "JPEG Images (*.jpg,*.jpeg)|*.jpg;*.jpeg|Gif Images (*.gif)|*.gif|Bitmaps (*.bmp)|*.bmp";

		#region Private members

		private ViewerControl mWFViewerControl;
		private string originalFilePath;
		private static CultureInfo[] SUPPORTED_CULTURES = new CultureInfo[] { new CultureInfo("hu-HU"), new CultureInfo("en-US") };
		private FileInfo [] picturesInFolder = new FileInfo[0];
		private int indexOfPictureInFolder = -1;
		private Timer mSlideShowTimer = new Timer();
		//MenuItemControl tsiMain;
		private bool mCyclicSlideShow = true;

		#endregion

		#region Constructor logic

		public Module(IModuleHost parent)
			: base(parent)
        {
			Name = "Image Viewer";
		}

        public override void Initialize()
        {
            base.Initialize();

            if (HostType != ModuleHostType.WindowsForms)
            {
                //TODO: log something
                return;
            }

			mWFViewerControl = new ViewerControl(this);
			pHost.Smartparts.Add(mWFViewerControl);
			
            mSlideShowTimer.Interval = 3000;
			mSlideShowTimer.Tick += new EventHandler(SlideShowTimer_Tick);
        }

		public override void InitializeMenus()
		{
            //tsiMain = new MenuItemControl();
            //tsiMain.Name = "tsiViewer";
            //tsiMain.Text = "Image Viewer";
            //tsiMain.AutoSize = true;

            //MenuItemControl tsi = new MenuItemControl();
            //tsi.Name = "tsiOpenPic";
            //tsi.Text = "Open...";
            //tsi.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.O)));
            //tsi.AutoSize = true;
            //tsi.Click += new System.EventHandler(open_Click);
            //tsiMain.DropDownItems.Add(tsi);

            //tsi = new MenuItemControl();
            //tsi.Name = "tsiReopenPic";
            //tsi.Text = "&Reopen";
            //tsi.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.R)));
            //tsi.AutoSize = true;
            //tsi.Enabled = false;
            //tsi.Click += new System.EventHandler(this.reopen_Click);
            //tsiMain.DropDownItems.Add(tsi);

            //tsi = new MenuItemControl();
            //tsi.Name = "tsiClosePic";
            //tsi.Text = "&Close";
            //tsi.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.C)));
            //tsi.AutoSize = true;
            //tsi.Enabled = false;
            //tsi.Click += new System.EventHandler(this.closeImage_Click);
            //tsiMain.DropDownItems.Add(tsi);

            //tsi = new MenuItemControl();
            //tsi.Name = "tsiSavePic";
            //tsi.Text = "&Save...";
            //tsi.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.S)));
            //tsi.AutoSize = true;
            //tsi.Enabled = false;
            //tsi.Click += new System.EventHandler(this.save_Click);
            //tsiMain.DropDownItems.Add(tsi);

            //tsi = new MenuItemControl();
            //tsi.Name = "tsiSavePicPart";
            //tsi.Text = "Save &visible part...";
            //tsi.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.P)));
            //tsi.AutoSize = true;
            //tsi.Enabled = false;
            //tsi.Click += new System.EventHandler(this.saveVisiblePart_Click);
            //tsiMain.DropDownItems.Add(tsi);

            //tsi = new MenuItemControl();
            //tsi.Name = "tsiPreviousImage";
            //tsi.Text = "Previous image";
            //tsi.AutoSize = true;
            //tsi.Enabled = false;
            //tsi.Click += new EventHandler(this.prevImg_Click);
            //tsiMain.DropDownItems.Add(tsi);

            //tsi = new MenuItemControl();
            //tsi.Name = "tsiNextImage";
            //tsi.Text = "Next image";
            //tsi.AutoSize = true;
            //tsi.Enabled = false;
            //tsi.Click += new EventHandler(this.nextImg_Click);
            //tsiMain.DropDownItems.Add(tsi);

            //tsi = new MenuItemControl();
            //tsi.Name = "tsiSlideShow";
            //tsi.Text = "SlideShow Start/Stop";
            //tsi.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Alt | System.Windows.Forms.Keys.S)));
            //tsi.AutoSize = true;
            //tsi.Enabled = false;
            //tsi.Click += new EventHandler(this.slideShow_Click);
            //tsiMain.DropDownItems.Add(tsi);

            //tsi = new MenuItemControl();
            //tsi.Name = "tsiCyclicSlideShow";
            //tsi.Text = "Cyclic SlideShow";
            //tsi.AutoSize = true;
            //tsi.Checked = true;
            //mCyclicSlideShow = tsi.Checked;
            //tsi.CheckOnClick = true;
            //tsi.Click += new EventHandler(this.cyclicSlideShow_Click);
            //tsiMain.DropDownItems.Add(tsi);

            //pHost.Menu.Add(tsiMain);
		}

        public override void InitializeSmartparts()
        {
            //throw new NotImplementedException();
        }

		#endregion

		#region Menu event handlers

		private void open_Click(object sender, EventArgs e)
		{
			OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
			openFileDialog.Filter = FILTER;
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				FillPictureListFromFolder(openFileDialog.FileName);
				//Open(new Uri(openFileDialog.FileName, UriKind.RelativeOrAbsolute));
                //tsiMain.DropDownItems["tsiReopenPic"].Enabled = true;
                //tsiMain.DropDownItems["tsiClosePic"].Enabled = true;
                //tsiMain.DropDownItems["tsiClosePic"].Enabled = true;
                //tsiMain.DropDownItems["tsiSavePic"].Enabled = true;
                //tsiMain.DropDownItems["tsiSavePicPart"].Enabled = true;
                //tsiMain.DropDownItems["tsiSlideShow"].Enabled = true;
                //tsiMain.DropDownItems["tsiPreviousImage"].Enabled = true;
                //tsiMain.DropDownItems["tsiNextImage"].Enabled = true;
			}
		}

		private void reopen_Click(object sender, EventArgs e)
		{
			//ReopenImage();
		}

		private void closeImage_Click(object sender, EventArgs e)
		{
            //tsiMain.DropDownItems["tsiReopenPic"].Enabled = false;
            //tsiMain.DropDownItems["tsiClosePic"].Enabled = false;
            //tsiMain.DropDownItems["tsiClosePic"].Enabled = false;
            //tsiMain.DropDownItems["tsiSavePic"].Enabled = false;
            //tsiMain.DropDownItems["tsiSavePicPart"].Enabled = false;
            //tsiMain.DropDownItems["tsiSlideShow"].Enabled = false;
            //tsiMain.DropDownItems["tsiPreviousImage"].Enabled = false;
            //tsiMain.DropDownItems["tsiNextImage"].Enabled = false;
			//CloseImage();
		}

		private void save_Click(object sender, EventArgs e)
		{
			SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
			saveFileDialog.Filter = FILTER;
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				//SaveImage(saveFileDialog.FileName);
			}
		}

		private void saveVisiblePart_Click(object sender, EventArgs e)
		{
			SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog();
			saveFileDialog.Filter = FILTER;
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				//SaveImagePart(saveFileDialog.FileName);
			}
		}

		private void slideShow_Click(object sender, EventArgs e)
		{
			if (mSlideShowTimer.Enabled)
			{
				//SlideShowStop();
			}
			else
			{
				//SlideShowStart();
			}
		}

		private void prevImg_Click(object sender, EventArgs e)
		{
			//PreviousImage();
		}

		private void nextImg_Click(object sender, EventArgs e)
		{
			//NextImage();
		}

		private void cyclicSlideShow_Click(object sender, EventArgs e)
		{
            //MenuItemControl mic = sender as MenuItemControl;

            //mCyclicSlideShow = mic.Checked;
		}
		
		#endregion

		#region Calling the methods...
        
        //protected override void Move(SBPweb.MediaCenter.Modules.Viewer.ModifyDescriptors.MoveDirections direction)
        //{
        //    mWFViewerControl.MoveImage(direction);
        //}

        //protected override void Flip(SBPweb.MediaCenter.Modules.Viewer.ModifyDescriptors.FlipAxis axis)
        //{
        //    mWFViewerControl.Flip(axis);
        //}

        //protected override void Rotate(SBPweb.MediaCenter.Modules.Viewer.ModifyDescriptors.RotateDeg deg)
        //{
        //    mWFViewerControl.Rotate(deg);
        //}

        //protected override void Zoom(SBPweb.MediaCenter.Modules.Viewer.ModifyDescriptors.ZoomStyles zoom)
        //{
        //    mWFViewerControl.Zoom(zoom);
        //}

        //public override void Open(Uri filePath)
        //{
        //    Image img = Image.FromFile(filePath.OriginalString);
        //    originalFilePath = filePath.OriginalString;
        //    mWFViewerControl.Image = img;
        //}

        //protected override void ReopenImage()
        //{
        //    if (string.IsNullOrEmpty(originalFilePath))
        //    {
        //        return;
        //    }
        //    Image img = Image.FromFile(originalFilePath);
        //    mWFViewerControl.Image = img;
        //}

        //protected override void CloseImage()
        //{
        //    originalFilePath = null;
        //    mWFViewerControl.Image = null;
        //}

        //protected override void SaveImage(string filePath)
        //{
        //    mWFViewerControl.Image.Save(filePath);
        //}

        //protected override void SaveImagePart(string filePath)
        //{
        //    mWFViewerControl.VisiblePartOfImage.Save(filePath);
        //}

        //protected override void NextImage()
        //{
        //    if (picturesInFolder.Length < 2)
        //    {
        //        return;
        //    }
        //    int originalValue = indexOfPictureInFolder;
        //    indexOfPictureInFolder++;
        //    if (indexOfPictureInFolder >= picturesInFolder.Length)
        //    {
        //        if (mCyclicSlideShow)
        //        {
        //            indexOfPictureInFolder = 0;
        //        }
        //        else
        //        {
        //            indexOfPictureInFolder = picturesInFolder.Length - 1;
        //        }
        //    }
        //    if (originalValue != indexOfPictureInFolder)
        //    {
        //        Open(new Uri(picturesInFolder[indexOfPictureInFolder].FullName, UriKind.RelativeOrAbsolute));
        //    }
        //}

        //protected override void PreviousImage()
        //{
        //    if (picturesInFolder.Length < 2)
        //    {
        //        return;
        //    }
        //    int originalValue = indexOfPictureInFolder;
        //    indexOfPictureInFolder--;
        //    if (indexOfPictureInFolder < 0)
        //    {
        //        if (mCyclicSlideShow)
        //        {
        //            indexOfPictureInFolder = picturesInFolder.Length - 1;
        //        }
        //        else
        //        {
        //            indexOfPictureInFolder = 0;
        //        }
        //    }
        //    if (originalValue != indexOfPictureInFolder)
        //    {
        //        Open(new Uri(picturesInFolder[indexOfPictureInFolder].FullName, UriKind.RelativeOrAbsolute));
        //    }
        //}

        //protected override void SlideShowStart()
        //{
        //    mSlideShowTimer.Enabled = true;
        //}

        //protected override void SlideShowStop()
        //{
        //    mSlideShowTimer.Enabled = false;
        //}

		#endregion

		private void FillPictureListFromFolder(string fileName)
		{
			if (string.IsNullOrEmpty(fileName))
			{
				picturesInFolder = new FileInfo[0];
				indexOfPictureInFolder = -1;
				return;
			}
			DirectoryInfo dirInfo = (new FileInfo(fileName)).Directory;
			string[] extensions = FILTER.Split('|');
			List<FileInfo> pictures = new List<FileInfo>();
			for (int i = 1; i < extensions.Length; i += 2)
			{
				string[] exts = extensions[i].Split(';');
				foreach (string ext in exts)
				{
					FileInfo[] currExt = dirInfo.GetFiles(ext);
					pictures.AddRange(currExt);
				}
			}
			picturesInFolder = pictures.ToArray();

			for (int i = 0; i < picturesInFolder.Length; i++)
			{
				if (picturesInFolder[i].FullName == fileName)
				{
					indexOfPictureInFolder = i;
					return;
				}
			}
		}

		private void SlideShowTimer_Tick(object sender, EventArgs e)
		{
			mSlideShowTimer.Enabled = false;
			if (picturesInFolder.Length < 2)
			{
				//SlideShowStop();
				return;
			}
			if (!mCyclicSlideShow && indexOfPictureInFolder >= picturesInFolder.Length - 1)
			{
				//SlideShowStop();
				return;
			}
			//NextImage();
			mSlideShowTimer.Enabled = true;
		}

        public override void LoadPersistency()
        {
            throw new NotImplementedException();
        }

        public override void SavePersistency()
        {
            throw new NotImplementedException();
        }

        public override void Terminate()
        {
            throw new NotImplementedException();
        }
    }
}
